package com.lcxw.hxzbapi.controller.giftandcubecontroller;

import com.alibaba.fastjson.JSON;
import com.lcxw.hxzbapi.entity.pojo.CubeResult;
import com.lcxw.hxzbapi.entity.pojo.Gift;
import com.lcxw.hxzbapi.entity.pojo.Ticket;
import com.lcxw.hxzbapi.service.GiftService;
import com.lcxw.hxzbapi.service.TicketService;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import com.netflix.ribbon.proxy.annotation.ResourceGroup;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbapi.entity.pojo.Cube;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.CubeService;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/api/cube")
@Slf4j
public class CubeController {


    @Resource
    CubeService cubeService;

    @Resource
    CubeRealization cubeRealization;

    @Resource
    GiftService giftService;

    @Resource
    TicketService ticketService;

    @Resource
    GiftRealization giftRealization;

    /**
     * 获取魔方的所有可供抽奖的礼物
     */
    @PostMapping("/getAll")
    public Response getAllCube(@RequestBody JSONObject requestParams) {

        long userId = Long.parseLong(requestParams.getString("userId"));
        String type = requestParams.getString("type");
        Response response = new Response();

        Map<String, Object> data = new LinkedHashMap<>();
        List<Cube> cubeList;
        Gift cubeGift = null;
        int ticket = giftRealization.getTicketTimes(userId, type);
        try {
            cubeList = cubeService.selectByType(type);
            if ("luckyCube".equals(type)) {
                cubeGift = giftService.selectByPrimaryKey(2);
            } else if ("diamondCube".equals(type)) {
                cubeGift = giftService.selectByPrimaryKey(3);
            }

        } catch (Exception e) {
            return Response.dbFail(log, "get all cube");
        }
        data.put("cubeList", cubeList);
        data.put("cubeGift", cubeGift);
        data.put("ticket", ticket);
        response.setData(data);
        response.setDesc("success");
        return response;

    }

    /**
     * 魔方抽奖接口
     */
    @PostMapping("/lucky/draw")
    public Response luckyDraw(@RequestBody JSONObject requestParams) {
        long userId = Long.parseLong(requestParams.getString("userId"));
        long roomId = Long.parseLong(requestParams.getString("roomId"));
        String type = requestParams.getString("type");
        int times = Integer.parseInt(requestParams.getString("times"));

        //查用户 type 类型下有多少抽奖次数
        int ticketTimes = giftRealization.getTicketTimes(userId, type);
        if (ticketTimes < times) {
            return Response.error(ErrorCode.INVALID_REQ, "抽奖次数不足");
        }
        JSONObject data = new JSONObject();
        List<CubeResult> cubeList = new ArrayList<>();

        //times次抽奖结果
        try {
            for (int i = 1; i <= times; i++) {
                CubeResult cubeResult = cubeRealization.luckyDraw(userId, roomId, type);
                cubeList.add(cubeResult);
            }
        } catch (Exception e) {
            return Response.dbFail(log, "数据库操作失败");
        }

        //抽奖结果进行查重
        HashMap<Integer, CubeResult> map = new HashMap<>();

        for (CubeResult bean : cubeList) {
            if (map.containsKey(bean.getPrizeIndex())) {
                bean.setNum(map.get(bean.getPrizeIndex()).getNum() + 1);
            }
            map.put(bean.getPrizeIndex(), bean);
        }
        cubeList.clear();
        cubeList.addAll(map.values());

        //抽奖完成后，再查询一下用户剩余的次数
        int ticket = giftRealization.getTicketTimes(userId, type);

        data.put("cubeList", cubeList);
        data.put("ticket", ticket);
        return Response.ok(data);

    }

}
